﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NDesk.Options;

namespace Obra.CommandLineInterface
{
    internal class ArgumentParserServiceProvider :
        IArgumentParserService
    {
        private readonly OptionSet optionSet;
        
        public ArgumentParserServiceProvider()
        {
            this.optionSet = new OptionSet();
            this.DefineArgumentsToParse();
            
            this.DryRun = false;
        }

        /// <summary>
        /// Media directory where Obra synchronizes with SmugMug from.
        /// </summary>
        public string Directory
        {
            get;
            set;
        }

        /// <summary>
        /// Name of the user to authenticate with.
        /// </summary>
        public string UserName
        {
            get;
            set;
        }

        /// <summary>
        /// Password of the user to authenticate with.
        /// </summary>
        public string Password
        {
            get;
            set;
        }

        /// <summary>
        /// Do not actually commit data to a synchronization provider, but show
        /// what would we committed.
        /// </summary>
        public bool DryRun
        {
            get;
            set;
        }

        /// <summary>
        /// Define the command line arguments to parse.
        /// </summary>
        private void DefineArgumentsToParse()
        {
            // u|username <string>
            this.optionSet.Add(
                Messages.UsernameParameter,
                Messages.UsernameParameterHelp,
                x => this.UserName = x);

            // p|password <string>
            this.optionSet.Add(
                Messages.PasswordParameter,
                Messages.PasswordParameterHelp,
                x => this.Password = x);

            // d|directory <string>
            this.optionSet.Add(
                Messages.DirectoryParameter,
                Messages.DirectoryParameterHelp,
                x => this.Directory = x);

            // dry-run
            this.optionSet.Add(
                Messages.DryRunParameter,
                Messages.DrynRunParameterHelp,
                x => this.DryRun = true);

            // h|?|help
            // V|version
        }

        /// <summary>
        /// Parse the command line arguments.
        /// </summary>
        public void Parse(string[] args)
        {
            this.optionSet.Parse(args);
        }
    }
}
